package com.example.commen.other

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.example.commen.R


class ItemDecoration : RecyclerView.ItemDecoration {
    private var mDivider: Drawable? = null //分割线Drawable

    private var inset = 0 //分割线缩进值

    /**
     * 使用line_divider中定义好的颜色
     *
     * @param context
     * @param dividerHeight 分割线高度
     */
    constructor(context: Context) {
        mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider)
    }

    constructor(context: Context, inset: Int) {
        this.inset = inset
        mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider)
    }

    /**
     * @param context
     * @param divider       分割线Drawable
     * @param dividerHeight 分割线高度
     */
    constructor(context: Context, divider: Drawable?, dividerHeight: Int) {
        mDivider = divider ?: ContextCompat.getDrawable(context, R.drawable.line_divider)
    }

    //获取分割线尺寸
    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        val left = parent.paddingLeft
        val right = parent.width - parent.paddingRight
        val childCount = parent.childCount
        //最后一个item不画分割线
        for (i in 0 until childCount - 1) {
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams
            val top = child.bottom + params.bottomMargin
            val bottom = top + mDivider!!.intrinsicHeight
            if (inset > 0) {
                mDivider!!.setBounds(left + inset, top, right - inset, bottom)
            } else {
                mDivider!!.setBounds(left, top, right, bottom)
            }
            mDivider!!.draw(c)
        }
    }
}